package com.huoniu.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huoniu.api.entity.Role;
import com.huoniu.api.mapper.RoleMapper;
import com.huoniu.api.service.RoleService;
import com.huoniu.common.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @类 名： RoleController <br/>
 * @描 述： <br/>
 * @日 期： 2020/9/4 14:06<br/>
 * @作 者： 侯建军<br/>
 * @版 本： 1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMapper roleMapper;

    @PostMapping
    public Result insert(@RequestBody  Role role){

        if(roleService.insert(role)){
            return Result.success("角色添加成功");
        }
        return  Result.fail(role.getRoleName()+"已存在！");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        if(roleService.delete(id)){
            return Result.success("删除成功");
        }
        return  Result.fail("删除失败");
    }

    @PutMapping
    public Result update(@RequestBody Role role){
        if(roleService.update(role)){
            return Result.success("修改成功");
        }
        return  Result.fail("修改失败");
    }

    @GetMapping
    public Result select(){
        QueryWrapper queryWrapper=new QueryWrapper();
        List<Role> roleList=roleMapper.selectList(queryWrapper);
        return Result.success(roleList);
    }
}
